﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using DataTools.Interop.Hardware;

namespace DataTools.Interop
{
    [Flags]
    internal enum FileIoAccess : uint
    {
        Read = (0x80000000),
        Write = (0x40000000),
        Execute = (0x20000000),
        All = (0x10000000),

        ReadData = (0x1),    // file & pipe
        ListDirectory = (0x1),    // directory

        WriteData = (0x2),    // file & pipe
        AddFile = (0x2),    // directory

        AppendData = (0x4),    // file
        AddSubDirectory = (0x4),    // directory
        CreatePipeInstance = (0x4),    // named pipe

        ReadEA = (0x8),    // file & directory

        WriteEA = (0x10),    // file & directory
        Traverse = (0x20),    // directory

        DeleteChild = (0x40),    // directory

        ReadAttributes = (0x80),    // all

        WriteAttributes = (0x100),    // all

        AllAccess = (IoCtl.STANDARD_RIGHTS_REQUIRED | IoCtl.SYNCHRONIZE | 0x1FF),

        GenericRead = (IoCtl.STANDARD_RIGHTS_READ |
                                           IoCtl.FILE_READ_DATA |
                                           IoCtl.FILE_READ_ATTRIBUTES |
                                           IoCtl.FILE_READ_EA |
                                           IoCtl.SYNCHRONIZE),

        GenericWrite = (IoCtl.STANDARD_RIGHTS_WRITE |
                                           IoCtl.FILE_WRITE_DATA |
                                           IoCtl.FILE_WRITE_ATTRIBUTES |
                                           IoCtl.FILE_WRITE_EA |
                                           IoCtl.FILE_APPEND_DATA |
                                           IoCtl.SYNCHRONIZE),

        GenericExecute = (IoCtl.STANDARD_RIGHTS_EXECUTE |
                                           IoCtl.FILE_READ_ATTRIBUTES |
                                           IoCtl.FILE_EXECUTE |
                                           IoCtl.SYNCHRONIZE),


    };

    [Flags]
    internal enum ShareModes : uint
    {
        Read = 1,
        Write = 2,
        Delete = 4
    };

    internal enum CreateDisposition : uint
    {
        CreateNew = 1,
        CreateAlways = 2,
        OpenExisting = 3,
        OpenAlways = 4,
        TruncateExisting = 5
    };

    [Flags]
    internal enum FileFlags : uint
    {
        WriteThrough = 0x80000000,
        Overlapped = 0x40000000,
        NoBuffering = 0x20000000,
        RandomAccess = 0x10000000,
        SequentialScan = 0x8000000,
        DeleteOnClose = 0x4000000,
        BackupSemantics = 0x2000000,
        PosixSemantics = 0x1000000,
        SessionAware = 0x800000,
        OpenReparsePoint = 0x200000,
        OpenNoRecall = 0x100000,
        FirstPipeInstance = 0x80000,
        OpenRequiringOpLock = 0x40000
    }


    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct OVERLAPPED
    {

        public IntPtr Internal;
        public IntPtr InternalHigh;
        public uint Offset;
        public uint OffsetHigh;
        public IntPtr hEvent;

        public long LongOffset
        {
            get
            {
                return Util.MakeLong(Offset, OffsetHigh);
            }
        }

    }

    internal static class FileIO
    {
        [DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode)]
        public static extern IntPtr CreateFile(
                [MarshalAs(UnmanagedType.LPWStr)]
                String lpFileName,
                FileIoAccess dwDesiredAccess,
                ShareModes dwShareMode,
                IntPtr lpSecurityAttributes,
                CreateDisposition dwCreationDisposition,
                FileFlags dwFlagsAndAttributes,
                IntPtr hTemplateFile
                );

        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr handle);

    }

}
